//
//  ZLViewControllerExtension.swift
//  ZLExtension
//
//  Created by 赵振磊 on 2024/11/19.
//

import Foundation

public extension UIViewController {
    class func zl_loadFromNib<T: UIViewController>(nibName: String? = nil) -> T {
        let nibName = nibName ?? String(describing: T.self)
        return T(nibName: nibName, bundle: Bundle(for: T.self))
    }
    
    class func zl_topVC(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = base as? UINavigationController {
            return zl_topVC(base: navigationController.visibleViewController)
        }
        if let tabBarController = base as? UITabBarController {
            return zl_topVC(base: tabBarController.selectedViewController)
        }
        if let presentedViewController = base?.presentedViewController {
            return zl_topVC(base: presentedViewController)
        }
        return base
    }
}
